﻿namespace MXERP.Blazor.Components
{
    public partial class TableRow<T> : MudComponentBase, IAsyncDisposable
    {
        [CascadingParameter]
        public Table<T> Table { get; set; } = default!;

        private bool _expanded;

        public bool Expanded
        {
            get => _expanded;
            set
            {
                if (_expanded != value)
                {
                    _expanded = value;
                }
            }
        }
        [Parameter, EditorRequired]
        public int Level { get; set; }
        [Parameter, EditorRequired]
        public T Item { get; set; } = default!;
        [Parameter, EditorRequired]
        public T? Parent { get; set; }

        public IEnumerable<T>? Children { get; set; }

        //public List<TableRow<T>> TableRowChildrens { get; set; } = new List<TableRow<T>>();

        internal void SetExpanded(bool expanded)
        {
            if (_expanded != expanded)
            {
                _expanded = expanded;
            }
        }

        public async ValueTask DisposeAsync()
        {
            await Table.SetSelectedItem(false, Item);
            Table.RemoveRow(this);
        }

        protected override void OnInitialized()
        {
            base.OnInitialized();
            Children = Table.TreeChildren(Item);
            Table.AddRow(this);
        }
    }
}